import common.CommonFunction;
import common.Mask;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

public var infoObject:Object;

[Embed(source="/images/success.png")]private var successIcon:Class;
[Embed(source="/images/error.png")]private var errorIcon:Class;
[Embed(source="/images/question.png")]private var questionIcon:Class;


public function onCreationComplete():void
{	
	infoObject = new Object();
	
	infoObject["userId"] =new Date();
	
	/**
	 * for call column in buffer_customer_info
	 **/ 
	infoObject["status"] = "1";
	
	Mask.show(CommonFunction.getConstants('pleaseWait'));	
	
	getRequests.send(infoObject);	
}

public var transReqequestsList:ArrayCollection;
public function approveTransactionRequests():void
{	
	transReqequestsList = new ArrayCollection();	
	for each (var obj:Object in customerDetailsGrid.dataProvider)
	{
		if(obj.select==true)
		{	
			transReqequestsList.addItemAt(obj.fromNumber,0);
			transReqequestsList.addItemAt(obj.toNumber,1);
			transReqequestsList.addItemAt(obj.amount,2);			
			if(obj.voucher=='Payment')
			{	
				transReqequestsList.addItemAt("P",3);
			}
			else if(obj.voucher=='Recieving')
			{				
				transReqequestsList.addItemAt("R",3);				
			}			
			transReqequestsList.addItemAt(obj.date,4);
			transReqequestsList.addItemAt(obj.serviceCharge,5);	
			transReqequestsList.addItemAt(obj.transactionId,6);				
		}
	}
	if(transReqequestsList.length==0)
	{	
		Alert.show(CommonFunction.getMessages('selectARecord'),	CommonFunction.getMessages('error'), 0, null, null);	
	}
	else 
	{	
		Alert.show(CommonFunction.getMessages('areyousure'), CommonFunction.getMessages('confirm'), (Alert.YES|Alert.NO), null, 
			onValidation);		
	}
}

public function onValidation(event:CloseEvent):void
{	
	if(event.detail==Alert.YES)
	{	
		infoObject = new Object();			
		infoObject["date"] = new Date();	
		infoObject["selectedTransactions"] = transReqequestsList;
		Mask.show(CommonFunction.getMessages('pleaseWait'));
		approveTransactions.send(infoObject);
	}	
}

public var requestsXML:XML;
public var requestsList:ArrayCollection;
public function onSuccess(event:ResultEvent):void
{	
	Mask.close();	
	requestsXML = event.result as XML;
	requestsList = new ArrayCollection();
	for each(var obj:Object in requestsXML.record)
	{	
		requestsList.addItem({select:true,fromNumber:obj.fromNumber,toNumber:obj.toNumber,
							amount:obj.amount,voucher:obj.voucherType,date:obj.date,
							serviceCharge:obj.serviceCharge,transactionId:obj.transactionId});	
	}
	customerDetailsGrid.dataProvider = requestsList;	
}

public var resultXML:XML;
public function onReqSuccess(event:ResultEvent):void
{	
	Mask.close();	
	resultXML = event.result as XML;
	if(resultXML.booleanvalue=="true")
	{
		Alert.show(CommonFunction.getMessages('reqSuccess'), "Success");
	}
	else
	{	
		Alert.show(CommonFunction.getMessages('reqFailure'), "Failure");
	}
	infoObject["date"] = new Date();
	/**
	 * for call column in buffer_customer_info
	 **/ 
	infoObject["status"] = "1";
	Mask.show(CommonFunction.getConstants('pleaseWait'));
	getRequests.send(infoObject);
}


public function onFailure(event:FaultEvent):void
{	
	Mask.close();	
	Alert.show("Connection Error..try again");
}